<template>
	<view class="container">
		<!-- 电池列表 -->
		<view class="battery">
			<view class="battery_item" v-for="(item,index) in list"
				:key="item.id" @click="check(item)" :class="check_item_code == item.item_code ? 'active' : ''">
				<image v-if="item.use_state == 0" src="../../static/img/index/true.png" mode="widthFix"></image>
				<image v-if="item.use_state == 1" src="../../static/img/index/false.png" mode="widthFix"></image>
				{{item.item_code}}
			</view>
		</view>
		<!-- 底部按钮 -->
		<view class="btn">
			<button type="default" @click="openDoor">开门</button>
		</view>
		<!-- 错误提示框-->
		<view class="mask" v-if="error">
			<view class="pay">
				<view class="tit">
					温馨提示
				</view>
				<view class="con">
					{{msg}}
				</view>
				<view class="button">
					<button type="default" class="exit" @click="close">确定</button>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		getMent,
		opendoor
	} from '../../api/api.js'
	export default {
		data() {
			return {
				list: [],
				error: false,
				check_item_code:''
			}
		},
		onLoad(options) {
			// 通过首页扫码进入
			if (options.code) {
				this.code = options.code
			}
			let token = uni.getStorageSync('token')
			this.getMentDetail()
		},
		onShow() {
		},
		methods: {
			check(item){
				this.check_item_code = item.item_code;
			},
			// 获取电池列表
			getMentDetail() {
				getMent({
					code: this.code
				}).then(res => {
					if (res.data.state == 0) {
						this.error = true
						this.msg = '该设备已离线，请使用其他设备。'
					} else {
						this.list = res.data.device_item
					}
				})
			},
			openDoor() {
				opendoor({
					code: this.code,
					item_code:this.check_item_code
				}).then(res => {
					uni.showModal({
						title: '提示',
						content: res.msg,
						showCancel: false,
						success: function(res) {
							
						}
					});
				});
			}
		}
	}
</script>

<style scoped lang="scss">
	.container {
		width: 100%;
		height: calc(100% - 10upx);
		padding-top: 10upx;
		background-color: #f6f6f6;
	}

	.battery {
		width: calc(100% - 60upx);
		height: 900upx;
		margin: 0 auto;
		display: flex;
		flex-flow: column;
		flex-wrap: wrap;

		.battery_item {
			background-color: #FFFFFF;
			box-sizing: border-box;
			width: 33.33%;
			height: 178upx;
			border: 1upx solid #f6f6f6;
			display: flex;
			justify-content: center;
			align-items: center;
			image {
				width: 25%;
			}

			&.active {
				background-color: rgba(62, 185, 255, 0.5);
			}
		}

	}


	.btn {
		width: calc(100% - 60upx);
		height: 90upx;
		border-radius: 45upx;
		position: fixed;
		bottom: 50upx;
		left: 30upx;
		overflow: hidden;

		button {
			width: 100%;
			height: 90upx;
			line-height: 90upx;
			color: #FFFFFF;
			background-color: #3fb8ff;
		}
	}

</style>
